﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.Windows;
using ClimateControl.Infrastructure;
using System.Globalization;

namespace ClimateControl.Controls.Converters
{
    public class VotesToUserList : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string userList = null;
            IEnumerable<Vote> votes = value as IEnumerable<Vote>;

            if (votes != null && votes.Any())
            {
                string newLine = Environment.NewLine;
                userList = votes
                    .Select(v =>
                        {
                            var dif = (DateTime.Now - v.Time);
                            return string.Format(culture, "{0} ({1}{2}s ago)", v.User, dif.Minutes > 0 ? string.Format("{0}m ", dif.Minutes) : "", dif.Seconds);
                        })
                    .Aggregate(((soFar, next) => string.Format(culture, "{0}{1}{2}", soFar, newLine, next)));
            }

            return userList;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
 	        throw new NotImplementedException();
        }
    }
}
